﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Microsoft.Practices.Unity;

using ePlanet.SingleWell.Services;
using ePlanet.ArchiveMange.Services;


namespace Web.Views.DataManage
{
    public partial class ArchiveStatisticsView : PageBase
    {

        [Dependency]
        public ISingleWellService SingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IAccountArchiveService AccountArchiveService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStartDate.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        protected void btnExecuteStatitics_Click(object sender, EventArgs e)
        {
            try
            {
                ViewState["startDate"] = txtStartDate.Text;
                ViewState["endDate"] = txtEndDate.Text;

                #region 单井统计
                DataTable table = SingleWellService.StatisticsSinglewell(txtStartDate.Text, txtEndDate.Text);
                if (table == null)
                {
                    ViewState["allwells"] = 0;
                    ViewState["allitems"] = 0;
                    ViewState["allpages"] = 0;
                }
                else
                {
                    ViewState["allwells"] = table.Rows[0]["allwells"];
                    ViewState["allitems"] = table.Rows[0]["allitems"];
                    ViewState["allpages"] = table.Rows[0]["allpages"];
                }
                #endregion

                #region 档案统计

                int sumCate = 0;
                int sumItem = 0;
                int sumElec = 0;
                int tempCount = 0;

                //文书
                List<string> infoList = GetArchiveStatisticsInfos("document_archive");
                ViewState["document_itemcount"] = infoList[0];
                ViewState["document_eleccount"] = infoList[1];
                ViewState["document_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumItem += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //科研
                infoList = GetArchiveStatisticsInfos("technology_archive");
                ViewState["tech_catecount"] = infoList[0];
                ViewState["tech_eleccount"] = infoList[1];
                ViewState["tech_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumCate += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //基建类
                infoList = GetArchiveStatisticsInfos("BULIDPROJECT_ARCHIVE");
                ViewState["build_catecount"] = infoList[0];
                ViewState["build_eleccount"] = infoList[1];
                ViewState["build_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumCate += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //油气勘探开发类
                infoList = GetArchiveStatisticsInfos("olisearch_archive");
                ViewState["oil_itemcount"] = infoList[0];
                ViewState["oil_eleccount"] = infoList[1];
                ViewState["oil_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumItem += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //会计类
                infoList = GetArchiveStatisticsInfos("ACCOUNT_VOUCHER_ARCHIVE");
                ViewState["account_catecount"] = infoList[0];
                ViewState["account_eleccount"] = infoList[1];
                ViewState["account_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumCate += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //设备类
                infoList = GetArchiveStatisticsInfos("EQUIPMENT_ARCHIVE");
                ViewState["equipment_catecount"] = infoList[0];
                ViewState["equipment_eleccount"] = infoList[1];
                ViewState["equipment_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumCate += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //声像类
                infoList = GetArchiveStatisticsInfos("audiovedio_archive");
                ViewState["audio_itemcount"] = infoList[0];
                ViewState["audio_eleccount"] = infoList[1];
                ViewState["audio_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumItem += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                //实物类
                infoList = GetArchiveStatisticsInfos("realkind_archive");
                ViewState["real_itemcount"] = infoList[0];
                ViewState["real_eleccount"] = infoList[1];
                ViewState["real_archivetype"] = infoList[2];
                int.TryParse(infoList[0], out tempCount);
                sumItem += tempCount;
                int.TryParse(infoList[1], out tempCount);
                sumElec += tempCount;

                ViewState["sum_catecount"] = sumCate;
                ViewState["sum_itemcount"] = sumItem;
                ViewState["sum_eleccount"] = sumElec;

                #endregion
            }
            catch (System.Exception ex)
            {
            	
            }
        }

        private List<string> GetArchiveStatisticsInfos(string tableName)
        {
            DataTable table = null;
            List<string> dataList = new List<string>();
            string errorMsg = "--";
            try
            {
                //实物类
                table = AccountArchiveService.StatisticsArchvieCount(tableName,
                    CurrentUser.Org_FullFondsNum, txtStartDate.Text, txtEndDate.Text);

                if (table != null && table.Rows.Count > 0)
                    dataList.Add(table.Rows[0][0].ToString());
                else
                    dataList.Add(errorMsg);
            }
            catch (System.Exception ex)
            {
                dataList.Add(errorMsg);
            }

            try
            {
                table = AccountArchiveService.StatisticsArchvieElecCount(tableName,
                CurrentUser.Org_FullFondsNum, txtStartDate.Text, txtEndDate.Text);
                if (table != null && table.Rows.Count > 0)
                    dataList.Add(table.Rows[0][0].ToString());
                else
                    dataList.Add(errorMsg);
            }
            catch (System.Exception ex)
            {
                dataList.Add(errorMsg);
            }
            
            try
            {
                table = AccountArchiveService.StatisticsArchvieType(tableName,
                CurrentUser.Org_FullFondsNum, txtStartDate.Text, txtEndDate.Text);
                if (table != null && table.Rows.Count > 0)
                {
                    List<string> typeList = new List<string>();
                    foreach (DataRow row in table.Rows)
                    {
                        typeList.Add(row[0].ToString());
                    }
                    dataList.Add(string.Join("/", typeList.ToArray()));
                }
                else
                    dataList.Add(errorMsg);

            }
            catch (System.Exception ex)
            {
                dataList.Add(errorMsg);
            }
            
            return dataList;
        }

    }
}